/*
   Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

   Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
   3. Neither the name of the Wiscom System nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_base.h"
#include "los_task.h"
#include "los_hwi.h"
#include "los_typedef.h"
#include "pcrambox.h"
#include "los_api_sclist.h"
#include "los_inspect_entry.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

#define NUM_OF_TEST 4
#define TEST_VALUE 828
UINT32 *sclist_head_address = NULL;// start addr of fft
UINT32 uwTask;
void sclist_pcram_init()
{
   UINT32 uwRet = LOS_OK;
   UINT32 uwBoxSize = 0x80000UL;

   exmc_pcram_init();
   LOS_pcramboxClr(OS_PCRAM_START_ADDR, OS_PCRAM_START_ADDR, uwBoxSize);
   uwRet = LOS_pcramboxInit((VOID *)OS_PCRAM_START_ADDR, uwBoxSize);
   if (uwRet != LOS_OK)
   {
       printf("sclist init error\r\n");
   }
   else
   {
       printf("1. sclist init success!!\r\n");
   }
}

void test_write_and_read(VOID)
{
	UINT32 *pRet = sclist_head_address;
	UINT32 uwRet;
	int test_index[NUM_OF_TEST] = {0,1000,10000,60000};
   UINT32 i,os;
   printf("5. write to 'Test':\r\n");
   for(i=0;i<NUM_OF_TEST;i++)
   {
   	os = test_index[i];
       *(pRet + os) = TEST_VALUE;
   }

   printf("6. read from 'Test':\r\n");
   UINT32 res;
   UINT8 temp_camp=0;
   for(i=0;i<NUM_OF_TEST;i++)
   {
   	   res = 0;
   	   os = test_index[i];
       res = *(pRet+os);
       printf("    %d",(UINT32)(os));
       printf(":\t");
       printf("%d",res);
       printf("\r\n\n");
       if(res!=TEST_VALUE)
       {
           temp_camp++;
       }
   }

   if (temp_camp==0)
   {
       dprintf("7. 'Test' read and write test success!\r\n");
       uwRet = LOS_InspectStatusSetByID(LOS_INSPECT_SCLIST, LOS_INSPECT_STU_SUCCESS);
       if (LOS_OK != uwRet)
       {
           dprintf("Set Inspect Status Err \r\n");
       }
       LOS_TaskDelete(uwTask);
   }
   else
   {
       dprintf("7. 'Test' read and write test failed!\r\n");
       dprintf("Err num is %d!\r\n",temp_camp);
       dprintf("Set Inspect Status Err \r\n");
       LOS_TaskDelete(uwTask);
   }
}

static VOID *scList_Entry(UINT32 uwParam)
{
//	UINT16 *pRet = NULL;// start addr of fft
	////SCYHF
    VIB *pRet = NULL;// start addr of fft
    UINT8 *flag, i;
    char name[] = "Test";
    i = 1;
    flag = &i;
    *flag = 1;
    pRet = SC_MALLOC(name, 0x70000UL, flag);
    sclist_head_address = pRet;
    printf("\r\n3. %s's pRet is %p\r\n", name, pRet);
    if(pRet == NULL)
    {
        printf("SC_MALLOC FAIL!\r\n");
    }
    else
    {
        printf("THE Identifier IS '%s'\r\n", name);
        // if(*flag == 1)
        // {
        //     printf("THe fft_table_sclist has been created, the start addr is %p\r\n", (UINT32)pRet);
        // }
        // else
        // {
        //     printf("The start addr of allocated fft_table_sclist is %p\r\n", (UINT32)pRet);
        //     delay_1ms(100);
        // }
        printf("4. SC_MALLOC success!\r\n");
    }

	test_write_and_read();

//    pRet = SC_FREE(name);
//    if(pRet==0)
//    {
//        printf("%s free success!\r\n", name);
//    }

   return NULL;
}

UINT32 Example_SCLIST(VOID)
{
   printf("\r\n--------4.19 SCLIST test start!--------\r\n");
   UINT32 uwRet = 0;

   TSK_INIT_PARAM_S stInitParam;

   sclist_pcram_init();

   stInitParam.pfnTaskEntry = scList_Entry;
   stInitParam.usTaskPrio = 9;
   stInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
   stInitParam.pcName = "scList";
   LOS_TaskLock();//lock task schedue
   uwRet = LOS_TaskCreate(&uwTask, &stInitParam);

   if (uwRet != LOS_OK)
   {
       dprintf("create sclist task failed!,error:%x \r\n",uwRet);
       return uwRet;
   }
   else
   {
       printf("2. create sclist task success!\r\n");
   }
   LOS_TaskUnlock();//unlock task schedue

   return LOS_OK;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
